# import speech_recognition as sr
#
# # 创建一个 Recognizer 对象
# recognizer = sr.Recognizer()
#
# # 打开并读取 mp3 文件
# audio_file = r"C:\Users\Administrator\Documents\WeChat Files\wxid_a8ll54i2gfk012\FileStorage\File\2023-09\U1 " \
#              r"Reading.mp3"
# with sr.AudioFile(audio_file) as source:
#     audio = recognizer.record(source)
#
# try:
#     # 使用 Google Web Speech API 进行语音识别
#     text = recognizer.recognize_google(audio)
#     print("转换结果：", text)
# except sr.UnknownValueError:
#     print("无法识别语音")
# except sr.RequestError as e:
#     print(f"请求错误：{e}")


import speech_recognition as sr
from pydub import AudioSegment

# 定义MP3文件和输出WAV文件的路径
mp3_file = r"C:\Users\Administrator\Documents\WeChat Files\wxid_a8ll54i2gfk012\FileStorage\File\2023-09\U1 Reading.mp3 "
wav_file = "YuToBen/output/output_audio.wav"

# 将MP3文件转换为WAV格式
audio = AudioSegment.from_mp3(mp3_file)
audio.export(wav_file, format="wav")

# 创建 Recognizer 对象
recognizer = sr.Recognizer()

# 打开并读取WAV文件
with sr.AudioFile(wav_file) as source:
    audio = recognizer.record(source)

try:
    # 使用Google Web Speech API进行语音识别
    text = recognizer.recognize_google(audio)
    print("转换结果：", text)
except sr.UnknownValueError:
    print("无法识别语音")
except sr.RequestError as e:
    print(f"请求错误：{e}")